/*
 ID: brodyzt1
 PROG: milk2
 LANG:C++
 */

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int abs2 (int num)
{
    if (num < 1) {
        return num *= -1;
    }
    else {
        return num;
    }
}

int lengthOfArray (int array[50000])
{
    int index = 0;
    while (array[index] != 0) {
        index +=1;
    }
    return index;
}

void swapItems (int index1, int index2, int array[50000], int array2[50000])
{
    int temp;
    temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    
    temp = array2[index1];
    array2[index1] = array2[index2];
    array2[index2] = temp;
}

bool arrayIsSorted (int array[50000], int numCows)
{
    bool isSorted = true;
    for (int i = 0; i < numCows - 1; i++) {
        if (array[i] > array[i+1]) {
            isSorted = false;
            break;
        }
    }
    return isSorted;
}

void bubbleSort (int array[50000], int array2[50000], int numCows)
{
    while (!arrayIsSorted(array, numCows)) {
        for (int i = 0; i < numCows - 1; i++) {
            if (array[i] > array[i+1]) {
                swapItems(i, i+1, array, array2);
            }
        }
    }
    
}

int main(int argc, const char * argv[])
{
    ifstream input;
    input.open("milk2.in");
    int numCows;
    input >> numCows;
    int startTimes[numCows];
    int stopTimes[numCows];
    
    for (int i = 0; i < numCows; i++) {
        input >> startTimes[i];
        input >> stopTimes[i];
    }
    bubbleSort(startTimes, stopTimes, numCows);
    for (int i = 0; i < numCows; i++) {
        cout << startTimes[i] << " " << stopTimes[i] << "\n";
    }
    int longestMilked = 0;
    int currentStart, currentEnd;
    currentStart = startTimes[0];
    currentEnd =  stopTimes[0];
    longestMilked = currentEnd - currentStart;
    cout << longestMilked;
    for (int i = 0; i < (numCows == 1 ? numCows: numCows - 1); i++) {
        if (currentEnd >= startTimes[i + 1]) {
            currentEnd = currentEnd > stopTimes[i+1] ? currentEnd : stopTimes[i+1];
        }
        else {
            if (currentEnd == 0) {
                currentEnd = stopTimes[i];
            }
            if (abs2(currentEnd - currentStart) > longestMilked) {
                longestMilked = abs2(currentEnd - currentStart);
            }
            
            cout << "\nCurrent End: " << currentEnd << " Current Start: " << currentStart;
            currentStart = startTimes[i+1];
            currentEnd = stopTimes[i+1];
        }
        if (i == numCows - 1 && longestMilked == 0) {
            longestMilked = stopTimes[numCows-1] - startTimes[0];
        }
    }
    
    
    int timeNotMilked = 0;
    int milkingUntil = stopTimes[0];
    for (int i = 0; i < numCows - 1; i++) {
        
        if (stopTimes[i] > milkingUntil) {
            milkingUntil = stopTimes[i];
        }
        if(milkingUntil < startTimes[i + 1])
        {
            if (startTimes[i + 1] - milkingUntil > timeNotMilked) {
                timeNotMilked = startTimes[i + 1] - stopTimes[i];
            }
        }
    }
    ofstream output;
    output.open("milk2.out");
    output << abs2(longestMilked)<< " " << timeNotMilked << "\n";
    input.close();
    output.close();
    return 0;
}

